package com.so206.controller.system;

import com.github.pagehelper.PageInfo;
import com.so206.plugins.MyCacheManager;
import com.so206.po.SystemCcConfig;
import com.so206.po.SystemCcRecord;
import com.so206.service.SystemCcService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
 * CC防护相关设置
 */
@Controller
@RequestMapping("/admin/cc")
public class SystemCcController {

    @Autowired
    private SystemCcService ccService;

    @Autowired
    private MyCacheManager cacheManager;

    /**
     * 防护记录和设置界面
     */
    @RequiresPermissions("cc_config")
    @RequestMapping("/list")
    public String edit(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "20") Integer rows,
                       @RequestParam(defaultValue = "false") Boolean ajax,
                       Model model, String ip, String url) {
        SystemCcConfig config = ccService.getconfig(1L);
        PageInfo<SystemCcRecord> record = ccService.find_record_by_page(rows, page, ip, url);
        model.addAttribute("bean", record);
        model.addAttribute("config", config);
        model.addAttribute("ip", ip);
        model.addAttribute("url", url);
        if (ajax) {
            return "admin/cc/list::pageNode";
        } else {
            return "admin/cc/list";
        }
    }

    /**
     * 更新配置
     */
    @RequiresPermissions("cc_config")
    @RequestMapping("/update_config")
    @ResponseBody
    public Map update_config(SystemCcConfig ccConfig) {
        HashMap<Object, Object> map = new HashMap<>();
        ccService.update_config(ccConfig);
        cacheManager.remove("config");
        map.put("msg", "更新成功!");
        map.put("code", 0);
        return map;
    }

    /**
     * 删除记录
     */
    @RequiresPermissions("cc_config")
    @RequestMapping("/delete")
    @ResponseBody
    public Map delete(Long id) {
        HashMap<Object, Object> map = new HashMap<>();
        ccService.delete_record_by_id(id);
        map.put("msg", "删除成功!");
        map.put("code", 0);
        return map;
    }

    /**
     * 删除记录 - 全部
     */
    @RequiresPermissions("cc_config")
    @RequestMapping("/deleteAll")
    @ResponseBody
    public Map deleteAll() {
        HashMap<Object, Object> map = new HashMap<>();
        ccService.delete_all_record();
        map.put("msg", "删除成功!");
        map.put("code", 0);
        return map;
    }

    /**
     * 查看记录
     */
    @RequiresPermissions("cc_config")
    @RequestMapping("/view")
    public String view(Long id, Model model) {
        SystemCcRecord id1 = ccService.find_by_id(id);
        model.addAttribute("model", id1);
        return "admin/cc/view";
    }


}
